<?php
/**
 * 用户管理
 * Created by PhpStorm.
 * @User: 王海东
 * @Date: 2018/12/25 9:50
 */

namespace app\adm\controller;

use app\common\model\CapitalLog;
use app\common\model\User;
use app\common\model\Capital;
use app\common\model\UserReal;

use think\Db;

class UserController extends BaseController
{
    /**
     * 查询用户列表
     * @author 王海东
     * @date
     * @return void
     * @throws \Exception
     */
    public function lists()
    {
        $in = $this->I([
            [
                "start_time|开始时间",
                null,
                "s",
            ],
            [
                "end_time|截止时间",
                null,
                "s",
            ],
            [
                "available|是否可用",       // yes no
                null,
                "s",
            ],
            [
                "real_status|是否实名认证",   //1审核中 2通过 3失败
                null,
                "s",
            ],
            [
                "name|用户名或手机号",
                null,
                "s",
            ],
            [
                "agent_name|代理商名称",
                null,
                "s",
            ],
            [
                "page|页码",
                DEF_PAGE_NO,
                "d",
            ],
            [
                "pageSize|页幅",
                DEF_PAGE_SIZE,
                "d",
            ]
        ]);

        $u = new User();

        $where = [
            'start_time'    => $in['start_time'],
            'end_time'      => $in['end_time'],
            'available'     => $in['available'],
            'name'          => $in['name'],
            'agent_name'    => $in['agent_name'],
            'real_status'   => $in['real_status'],
        ];

        $data = $u->list(UID,$where,$in['page'],$in['pageSize']);

        rjd($data);
    }

    /**
     * 修改用于余额
     * @author 王海东
     * @date
     * @return void
     * @throws \Exception
     */
    public function updateBalance()
    {
        $in = $this->I([
            [
                "user_id|用户id",
                null,
                "d",
                "require",
            ],
            [
                "balance|修改金额",
                null,
                "f",
                "require",
            ]
        ]);

        $u      = new User();
        $info   = $u->info($in['user_id']);
        if(!$info) {
            rj(1,'用户不存在');
        }


        $c  = new Capital();
        $cl = new CapitalLog();

        if($in['balance'] >= 0) {
            $type   = 1;
            $msg    = '充值金额【'.$in['balance'].'】元';
        } else {
            $type   = 2;
            $msg    = '提现金额【'.$in['balance'].'】元';
        }

        Db::startTrans();

        try {
            $res = $c->setBalance($in['user_id'],$in['balance']);
            if(!$res) {
                Db::rollback();
                rj(1,'余额修改失败');
            }

            $cl->add($in['user_id'],$in['balance'],$msg,$type);
            Db::commit();
            rj();
        } catch (\Exception $e) {
            Db::rollback();
            rj(1,'余额修改失败');
        }
    }

    /**
     * 查询实名认证
     * @author 王海东
     * @date
     * @return void
     * @throws \Exception
     */
    public function userReal()
    {
        $in = $this->I([
            [
                "user_id|用户id",
                null,
                "d",
                "require",
            ]
        ]);
        $ur     = new UserReal();
        $data   = $ur->find($in['user_id']);

        rjd($data);
    }

    /**
     * 修改实名认证审核状态
     * @author 王海东
     * @date
     * @return void
     * @throws \Exception
     */
    public function editReal()
    {
        $in = $this->I([
            [
                "user_id|用户id",
                null,
                "d",
                "require",
            ],
            [
                "status|状态",
                null,
                "d",
                "require",
            ]
        ]);
        $ur     = new UserReal();
        if(!$ur->edit($in['user_id'],['status' => $in['status']])) {
            rj(1,'修改失败');
        }

        rj();
    }

    /**
     * 冻结用户
     * @author 王海东
     * @date
     * @return void
     * @throws \Exception
     */
    public function frozen()
    {
        $in = $this->I([
            [
                "user_id|用户id",
                null,
                "s",
                "require",
            ]
        ]);

        $u = new User();

        if(!$u->disable([$in['user_id']])) {
            rj(1,'冻结失败');
        }

        rj();
    }

    /**
     * 用户详情
     * @author 王海东
     * @date
     * @return void
     * @throws \Exception
     */
    public function info()
    {
        $in = $this->I([
            [
                "user_id|用户id",
                null,
                "d",
                "require",
            ]
        ]);

        $u      = new User();
        $info   = $u->info($in['user_id']);

        rjd($info);
    }
}